package com.user.servlet;

import com.user.entity.Users;
import com.user.service.IUserService;
import com.user.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpdateUserInfoServlet", value = "/UpdateUserInfoServlet")
public class UpdateUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = (String) request.getSession().getAttribute("username");
        String oldpassword = request.getParameter("oldpassword");
        String newpassword = request.getParameter("newpassword");
        IUserService userService = new UserServiceImpl();
        boolean b = userService.queryPasswordIsTure(username, oldpassword);
        if (!b) {
            request.setAttribute("msg", "旧密码错误，忘记密码请联系管理员！");
            request.getRequestDispatcher("User/updateUI.jsp").forward(request, response);

        }
        boolean b1 = userService.changeUserPassword(new Users(username, newpassword));
        if (b1) {
            request.setAttribute("msg", "修改密码成功！");
            request.getRequestDispatcher("User/updateUI.jsp").forward(request, response);

        } else
            request.setAttribute("msg", "修改密码失败！");

        request.setAttribute("msg", "");
        request.getRequestDispatcher("User/updateUI.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
